import isPlainObject from 'is-plain-object'

/**
 * 处理 Store 的 data list
 * @param {Object} source { list, data }
 * @param {Array} source.list ['id_1', 'id_2']
 * @param {Object} source.data {
 *    [source.list[0]]: 'value 1',
 *    [source.list[1]]: 'value 2',
 * }
 */
export default function handleDataList (source = {}) {
  if (!source || !isPlainObject(source)) {
    return []
  }

  const { list = [], data = {} } = source

  if (!Array.isArray(list) || list.length < 1) {
    return []
  }

  if (!isPlainObject(data)) {
    return []
  }

  return list.map(it => data[it]).filter(it => it !== undefined)
}
